package com.ysd.twocrm.websocket;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.Roles;
import com.ysd.twocrm.entity.Users;
import com.ysd.twocrm.mapper.RolesMapper;
import com.ysd.twocrm.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("websocketverify")
public class WebScketVerify {
    @Autowired
    private RolesMapper rolesMapper;
    @Autowired
    private UsersMapper usersMapper;

    @GetMapping
    public Object getIfUser(Integer userId){

        List<Roles> roles = rolesMapper.getAllIfRolesByUserId(userId);//根据用户id来查询当前的角色
        for(int i=0;i<roles.size();i++){
            if(roles.get(i).getRoleName().equals("游客")){
                //返回当前游客对应的咨询师
                Users zixunshi = usersMapper.findAllIsCounselor(userId);
                List<Users> usersList=new ArrayList<>();
                usersList.add(zixunshi);
                return usersList;
            }else if(roles.get(i).getRoleName().equals("咨询师")){
                //返回咨询师对应的游客
                QueryWrapper<Users> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("IsCounselor",userId);
                List<Users> youke = usersMapper.selectList(queryWrapper);
                return youke;
            }
        }
        return null;
    }
    /*
     * @Description 拿到用户id先去判断他是否拥有咨询师，
     * 首先会先去判断身份，如果是游客，并且游客身份没有分配咨询师的话进行处理
     * @param userId
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/18 8:53
     */
    @GetMapping("/1")
    public Object getAllIfUsers(Integer userId){

        List<Roles> roles = rolesMapper.getAllIfRolesByUserId(userId);//根据用户id来查询当前的角色
        QueryWrapper<Users> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        Users users = usersMapper.selectOne(queryWrapper);
        for(int i=0;i<roles.size();i++){
            if(roles.get(i).getRoleName().equals("游客")){
                if(users.getIsCounselor()==null || users.getIsCounselor()==0){
                    return "success";
                }

            }
        }

        return "false";
    }
}
